package com.danger.Model.action;

import android.content.Context;
import android.text.TextUtils;
import com.danger.Model.bean.UserInfoBean;
import com.danger.Model.listener.OnRegisterListener;
import com.danger.Model.listener.OnSendCheckNumListener;
import com.danger.Model.listener.OnVerifyCheckNumListener;
import com.danger.Util.CheckCode;
import com.danger.Util.CheckMobileAndEmail;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;

/**
 * Created by danger on 2016/3/18.
 */
public class BindMobileModelImpl implements BindMobileModel {

    private Context context;
    private String PHONE_NUMBER_IS_EMPTY = "电话号码为空";
    private String CHECK_NUM_IS_EMPTY = "验证码为空";
    private String INFO_IS_RIGHT = "正确";
    private String PHONE_NUMBER_IS_WRONG = "电话号码错误";
    private boolean send;
    private boolean verify;
    private boolean flag;

    public BindMobileModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public String checkPhone(String phone) {
        if (TextUtils.isEmpty(phone)) {
            return PHONE_NUMBER_IS_EMPTY;
        }
        if (!CheckMobileAndEmail.isMobileNO(phone)) {
            return PHONE_NUMBER_IS_WRONG;
        }
        return INFO_IS_RIGHT;
    }

    @Override
    public String check(String phone, String checkNum) {
        if (TextUtils.isEmpty(phone)) {
            return PHONE_NUMBER_IS_EMPTY;
        }
        if (TextUtils.isEmpty(checkNum)) {
            return CHECK_NUM_IS_EMPTY;
        }
        return INFO_IS_RIGHT;
    }

    @Override
    public void sendChecknum(String phone, final OnSendCheckNumListener onSendCheckNumListener) {
        BmobSMS.requestSMSCode(context, phone, "MESSAGE", new RequestSMSCodeListener() {
            @Override
            public void done(Integer integer, BmobException e) {
                if (e == null) {
                    send = true;
                    onSendCheckNumListener.onSuccess();
                } else {
                    send = false;
                    onSendCheckNumListener.onError(e);
                }
            }
        });
    }

    @Override
    public boolean verifyChecknum(String phone, String checkNum, final OnVerifyCheckNumListener onVerifyCheckNumListener) {
        if (send) {
            BmobSMS.verifySmsCode(context, phone, checkNum, new VerifySMSCodeListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        verify = true;
                        onVerifyCheckNumListener.onSuccess();
                    } else {
                        verify = false;
                        onVerifyCheckNumListener.onError(e);
                    }
                }
            });
        } else {
            verify = false;
        }
        return verify;
    }

    @Override
    public boolean register(UserInfoBean userInfo, String phone, final OnRegisterListener listener) {
        if (verify) {
            userInfo.setMobilePhoneNumber(phone);
            userInfo.setMobilePhoneNumberVerified(true);
            userInfo.signUp(context, new SaveListener() {
                @Override
                public void onSuccess() {
                    flag = true;
                    listener.onSuccess();
                }

                @Override
                public void onFailure(int i, String s) {
                    flag = false;
                    listener.onError(CheckCode.checkByCode(i));
                }
            });
        } else {
            flag = false;
        }
        return flag;
    }
}
